﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.BaseData
{
    public partial class EditTeacherInfo : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");
            strPagePrivilege.Add("JSSJ");

            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] == null)
                {
                    Common.JShelper.JSAlert(this.Page, "", "页面出错！");
                }
                else
                {
                    int ID = int.Parse(Request.QueryString["ID"].ToString());
                    InitBindDDl();
                    InitBindFromDB(ID);
                }
                if (Request.QueryString["flag"] == "look")
                {
                    label1.Visible = false;
                    btnCancel.Visible = false;
                    FlagHF.Value = "look";
                }
            }
            tbCode.Attributes.Add("readOnly", "true");
            tbcsrq.Attributes.Add("readOnly", "true");
            tbgzrq.Attributes.Add("readOnly", "true");
            tbbysj.Attributes.Add("readOnly", "true");

            tbcsrq.Attributes.Add("OnClick", "c.showMoreDay=false;c.show(this);");
            tbgzrq.Attributes.Add("OnClick", "c.showMoreDay=false;c.show(this);");
            tbbysj.Attributes.Add("OnClick", "c.showMoreDay=false;c.show(this);");
        }

        /// <summary>
        /// 根据传递过来的ID从数据库中读出初始值
        /// </summary>
        /// <param name="ID"></param>
        protected void InitBindFromDB(int ID)
        {
            string str = "";
            LabMS.BLL.Teacher BTeacher = new LabMS.BLL.Teacher();
            LabMS.Model.Teacher MT = BTeacher.GetModel(ID);

            tbkpCode.Text = MT.CardCode;
            tbcs.Text = MT.ComeFrom;
            tbcsrq.Text = MT.Dirthday == null ? "" : MT.Dirthday.Value.ToString();
            tbwhcd.Text = MT.Education;
            tbEmail.Text = MT.Email;
            tbFax.Text = MT.Fax;
            tbbysj.Text = MT.GDate == null ? "" : MT.GDate.Value.ToString();
            tbbyxx.Text = MT.GSchool;
            tbjkzk.Text = MT.Health;
            tbhomeAdd.Text = MT.HomeAddress;
            tbhomeTel.Text = MT.Homephone;
            tbzw.Text = MT.Jobs;
            tbhyzk.Text = MT.Marriage;
            tbPhone.Text = MT.Mobile;
            tbmz.Text = MT.Nation;
            tbOfficeAdd.Text = MT.OfficeAddress;
            tbzzmm.Text = MT.Political;
            tbjszc.Text = MT.Professional;
            tbPro.Text = MT.Province;
            ddlSex.SelectedValue = MT.SexCode;
            tbzyjstc.Text = MT.Specially;
            tbCode.Text = MT.Teacher_Code;
            tbName.Text = MT.Teacher_Name;
            tbOfficeTel.Text = MT.Tel;
            tbgzrq.Text = MT.WorkDate == null ? "" : MT.WorkDate.Value.ToString();
            tbPassword.Text = MT.Password;
        }


        /// <summary>
        /// 初始化性别下拉框绑定
        /// </summary>
        protected void InitBindDDl()
        {
            LabMS.BLL.Dictionary bd = new LabMS.BLL.Dictionary();
            string str = "DClass='201'";
            ddlSex.DataSource = bd.GetModelList(str);
            ddlSex.DataValueField = "Code";
            ddlSex.DataTextField = "Name";
            ddlSex.DataBind();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(tbCode.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "教师编号不能为空！");
                tbCode.Focus();
            }
            else if (string.IsNullOrEmpty(tbName.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "老师名称不能为空！");
                tbName.Focus();
            }
            else if (string.IsNullOrEmpty(tbPassword.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "密码不能为空！");
                tbPassword.Focus();
            }
            #endregion
            else
            {
                LabMS.Model.Teacher MT = new LabMS.Model.Teacher();
                MT.CardCode = tbkpCode.Text.Trim();
                MT.ComeFrom = tbcs.Text.Trim();
                if (tbcsrq.Text.Trim() != "")
                {
                    MT.Dirthday = DateTime.Parse(tbcsrq.Text.Trim());
                }
                MT.Education = tbwhcd.Text.Trim();
                MT.Email = tbEmail.Text.Trim();
                MT.Fax = tbFax.Text.Trim();
                if (tbbysj.Text.Trim() != "")
                {
                    MT.GDate = DateTime.Parse(tbbysj.Text.Trim());
                }
                MT.GSchool = tbbyxx.Text.Trim();
                MT.Health = tbjkzk.Text.Trim();
                MT.HomeAddress = tbhomeAdd.Text.Trim();
                MT.Homephone = tbhomeTel.Text.Trim();
                MT.Jobs = tbzw.Text.Trim();
                MT.Marriage = tbhyzk.Text.Trim();
                MT.Mobile = tbPhone.Text.Trim();
                MT.Nation = tbmz.Text.Trim();
                MT.OfficeAddress = tbOfficeAdd.Text.Trim();
                MT.Political = tbzzmm.Text.Trim();
                MT.Professional = tbjszc.Text.Trim();
                MT.Province = tbPro.Text.Trim();
                MT.SexCode = ddlSex.SelectedValue.ToString();
                MT.Specially = tbzyjstc.Text.Trim();
                MT.Teacher_Code = tbCode.Text.Trim();
                MT.Teacher_Name = tbName.Text.Trim();
                MT.Tel = tbOfficeTel.Text.Trim();
                MT.Password = tbPassword.Text.Trim();
                if (tbgzrq.Text.Trim() != "")
                {
                    MT.WorkDate = DateTime.Parse(tbgzrq.Text.Trim());
                }
                MT.ID = int.Parse(Request.QueryString["ID"].ToString());
                LabMS.BLL.Teacher BT = new LabMS.BLL.Teacher();
                try
                {
                    BT.Update(MT);
                    if (FlagHF.Value == "look")
                    {
                        Common.JShelper.JsAlertAndClose(Page, "", "修改成功！");
                    }
                    else
                    {

                        Common.JShelper.JSAlertAndRedirect(this.Page, "", "修改成功！", "BaseDataTeacherList.aspx");
                    }
                }
                catch
                {
                    Common.JShelper.JSAlert(this.Page, "", "修改失败！");
                }
            }
        }
    }
}
